package com.game.battleship.data;

import java.util.List;

import com.game.battleship.generator.AllShipPositionsForbiddenException;
import com.game.battleship.generator.RandomShipGenerator;

/**
 * Class to generate ship formation. Supply 2 options, either hard-code formation or 
 * random formation
 * 
 * @author vtkhanh
 *
 */
public class FormationGenerator {
	
	private RandomShipGenerator generator;
	
	public FormationGenerator() {
		generator = new RandomShipGenerator(Game.BOARD_SIDE);
	}
	
	public static short[] getHardcodedFormation() {
		short[] formation = {
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.SHIP_4, Game.SHIP_4, Game.SHIP_4, Game.SHIP_4, Game.SHIP_4, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.SHIP_3, Game.SHIP_3, Game.SHIP_3, Game.SHIP_3, Game.WATER, Game.SHIP_1, Game.SHIP_1, Game.WATER, Game.WATER,
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.SHIP_2, Game.SHIP_2, Game.SHIP_2, Game.WATER, Game.SHIP_2, Game.SHIP_2, Game.SHIP_2, Game.WATER, Game.WATER,
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
				Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER, Game.WATER,
			};
		return formation;
	}
	
	/**
	 * 
	 * @return hard-coded ship positions
	 */
	public static Ship[] getHardcodedShipPosition(){
		Ship[] resultShips = new Ship[Game.SHIPS_COUNT]; //5
		
		Ship s1 = new Ship((short) 2,new int[]{1,2});
		Ship s2 = new Ship((short) 2,new int[]{4,5});
		Ship s3 = new Ship((short) 3,new int[]{7,8,9});
		Ship s4 = new Ship((short) 4,new int[]{20,21,22,23});
		Ship s5 = new Ship((short) 5,new int[]{25,26,27,28,29});
		
		resultShips[0] = s1;
		resultShips[1] = s2;
		resultShips[2] = s3;
		resultShips[3] = s4;
		resultShips[4] = s5;
		
		return resultShips;
	}
	
	/**
	 * 
	 * @return - randomly generated ship positions
	 */
	public Ship[] getRandomShipsPosition() {		
		short currentShipSize;
		ShipCellsHolder tempSFH;
		List<Short> tempList;
		int[] tempArray;
		Ship[] resultShips = new Ship[Game.SHIPS_COUNT];
		
		for (int i = 0 ; i < Game.SHIPS_SIZES.length ; i++) {
			currentShipSize = Game.SHIPS_SIZES[i];
			try {
				tempSFH = generator.generateRandomShipPosition(currentShipSize);
				tempList = ShipCellsHolder.getShipFields(tempSFH, Game.BOARD_SIDE);
				tempArray = new int[tempList.size()];
				for (int j = 0 ; j < tempList.size() ; j++) {
					tempArray[j] = tempList.get(j);
				}
				resultShips[i] = new Ship(currentShipSize,tempArray);
				
				//DO NOT forget to update generator with the currently randomized ship, or else mShips may intersect
				generator.addShipUpdateState(tempSFH);
			} catch (AllShipPositionsForbiddenException e) {
				// TODO : design and implement Exception handling mechanism when all ship positions are forbidden and another ship position is yet to be generated
				e.printStackTrace();
			}
		}
		
		return resultShips;
	}

}
